https://leetcode-cn.com/problems/number-of-islands/


class Solution {
public:
    int dx[4] = {0,1,0,-1};
    int dy[4] = {1,0,-1,0};
    void dfs(vector<vector<char>>& grid,int i,int j)
    {
        int n = grid.size();
        int m = grid[0].size();
        grid[i][j] = '0';
        for(int k = 0 ; k < 4 ;k++)
        {
            int newi = i + dx[k];
            int newj = j + dy[k];
            if(newi < 0 || newj < 0 || newi >= n || newj >= m) continue;
            if(grid[newi][newj] == '1')
                dfs(grid,newi,newj);
        }
    }
    int numIslands(vector<vector<char>>& grid) {
        int n = grid.size();
        if(n == 0) return 0;
        int m = grid[0].size();
        int count = 0;
        for(int i = 0 ; i < n ;i++)
        {
            for(int j = 0 ; j < m ;j++)
            {
                if(grid[i][j] == '1')
                {
                    count++;
                    dfs(grid,i,j);
                }
            }
        }
        return count;
    }
};
